/*
 * @author: wizardc
 */

using Dou.ScreenController;
using Dou.Utils;
using UnityEditor;
using UnityEngine;

namespace DouEditor.ScreenController
{
    /// <summary>
    /// 屏幕控制窗口
    /// </summary>
    public class ScreenControllerWindow : EditorWindow
    {
        [MenuItem("DouFramework/UI/横竖屏和多语言适配")]
        public static void ShowWindow()
        {
            EditorWindow window = GetWindow<ScreenControllerWindow>(false, "横竖屏和多语言适配");
            window.minSize = new Vector2(550, 400);
            window.autoRepaintOnSceneChange = true;
        }

        private GUIStyle _boxStyle;

        private float _leftOrTopPercent = 0.05f;
        private float _rightOrBottomPercent = 0.03f;
        
        private void OnGUI()
        {
            if (_boxStyle == null)
            {
                _boxStyle = new GUIStyle(GUI.skin.box);
                _boxStyle.alignment = TextAnchor.MiddleLeft;
                _boxStyle.fontSize = 13;
                _boxStyle.richText = true;
            }
            
            using (new EditorGUILayout.VerticalScope())
            {
                if (!EditorApplication.isPlaying)
                {
                    GUILayout.Box(new GUIContent("横竖屏和多语言适配说明：\n1. 该功能仅对Dou.ScreenController命名空间下的组件有效；\n2. 项目有横竖屏切换或多语言切换的需求时才使用该工具包；\n3. 横竖屏切换时UI会大改；\n4. 多语言切换时UI会出现微调，文本和UI不能完全匹配时；"), _boxStyle, GUILayout.ExpandWidth(true), GUILayout.Height(95));
                    
                    using (new EditorGUILayout.HorizontalScope())
                    {
                        EditorGUILayout.LabelField("UI设计尺寸（横屏）", GUILayout.Width(120));
                        using (new EditorGUILayout.VerticalScope())
                        {
                            var designWidth = ScreenControllerMgr.GetLandscapeDesignWidth();
                            var newDesignWidth = EditorGUILayout.IntSlider("宽度", designWidth, 500, 5000);
                            if (newDesignWidth != designWidth)
                            {
                                ScreenControllerMgr.SetLandscapeDesignWidth(newDesignWidth);
                                OnDesignSizeChanged();
                            }
                            
                            var designHeight = ScreenControllerMgr.GetLandscapeDesignHeight();
                            var newDesignHeight = EditorGUILayout.IntSlider("高度", designHeight, 500, 5000);
                            if (newDesignHeight != designHeight)
                            {
                                ScreenControllerMgr.SetLandscapeDesignWidth(newDesignHeight);
                                OnDesignSizeChanged();
                            }

                            var matchWidthOrHeight = ScreenControllerMgr.GetLandscapeMatchWidthOrHeight();
                            var newMatchWidthOrHeight = EditorGUILayout.Slider("matchWidthOrHeight", matchWidthOrHeight, 0, 1);
                            if (!MathUtil.CheckEqual(matchWidthOrHeight, newMatchWidthOrHeight))
                            {
                                ScreenControllerMgr.SetLandscapeMatchWidthOrHeight(newMatchWidthOrHeight);
                                OnDesignSizeChanged();
                            }
                        }
                    }
                    using (new EditorGUILayout.HorizontalScope())
                    {
                        EditorGUILayout.LabelField("屏幕方向", GUILayout.Width(120));

                        var uiScreenMode = ScreenControllerMgr.GetScreenMode();
                        var originColor = GUI.color;
                        
                        if (uiScreenMode == UIScreenMode.Landscape)
                        {
                            GUI.color = Color.yellow;
                        }
                        if (GUILayout.Button("横屏", GUILayout.Width(100)))
                        {
                            if (uiScreenMode != UIScreenMode.Landscape)
                            {
                                ScreenControllerMgr.SetScreenMode(UIScreenMode.Landscape);
                                OnScreenModeChanged(UIScreenMode.Landscape);
                            }
                        }
                        GUI.color = originColor;
                        if (uiScreenMode == UIScreenMode.Portrait)
                        {
                            GUI.color = Color.yellow;
                        }
                        if (GUILayout.Button("竖屏", GUILayout.Width(100)))
                        {
                            if (uiScreenMode != UIScreenMode.Portrait)
                            {
                                ScreenControllerMgr.SetScreenMode(UIScreenMode.Portrait);
                                OnScreenModeChanged(UIScreenMode.Portrait);
                            }
                        }
                        GUI.color = originColor;
                    }
                    using (new EditorGUILayout.HorizontalScope())
                    {
                        EditorGUILayout.LabelField("多语言", GUILayout.Width(120));

                        var uiFineTuningMode = ScreenControllerMgr.GetFineTuningMode();
                        var originColor = GUI.color;
                        
                        if (uiFineTuningMode == UIFineTuningMode.None)
                        {
                            GUI.color = Color.cyan;
                        }
                        if (GUILayout.Button("无", GUILayout.Width(100)))
                        {
                            if (uiFineTuningMode != UIFineTuningMode.None)
                            {
                                ScreenControllerMgr.SetFineTuningMode(UIFineTuningMode.None);
                                OnFineTuningModeChanged(UIFineTuningMode.None);
                            }
                        }
                        GUI.color = originColor;
                        if (uiFineTuningMode == UIFineTuningMode.CN)
                        {
                            GUI.color = Color.cyan;
                        }
                        if (GUILayout.Button("简中", GUILayout.Width(100)))
                        {
                            if (uiFineTuningMode != UIFineTuningMode.CN)
                            {
                                ScreenControllerMgr.SetFineTuningMode(UIFineTuningMode.CN);
                                OnFineTuningModeChanged(UIFineTuningMode.CN);
                            }
                        }
                        GUI.color = originColor;
                        if (uiFineTuningMode == UIFineTuningMode.TC)
                        {
                            GUI.color = Color.cyan;
                        }
                        if (GUILayout.Button("繁中", GUILayout.Width(100)))
                        {
                            if (uiFineTuningMode != UIFineTuningMode.TC)
                            {
                                ScreenControllerMgr.SetFineTuningMode(UIFineTuningMode.TC);
                                OnFineTuningModeChanged(UIFineTuningMode.TC);
                            }
                        }
                        GUI.color = originColor;
                        if (uiFineTuningMode == UIFineTuningMode.EN)
                        {
                            GUI.color = Color.cyan;
                        }
                        if (GUILayout.Button("英文", GUILayout.Width(100)))
                        {
                            if (uiFineTuningMode != UIFineTuningMode.EN)
                            {
                                ScreenControllerMgr.SetFineTuningMode(UIFineTuningMode.EN);
                                OnFineTuningModeChanged(UIFineTuningMode.EN);
                            }
                        }
                        GUI.color = originColor;
                    }
                }

                if (EditorApplication.isPlaying)
                {
                    GUILayout.Box(new GUIContent("运行时说明：\n1. Game窗口的屏幕方向根据宽高比自动切换，无需设置；\n2. 运行时Game窗口切换为模拟器时会出现异常，请重新启动；\n3. 在Game窗口为非模拟器时，可以使用刘海屏模拟的功能来对刘海屏的适配进行测试；"), _boxStyle, GUILayout.ExpandWidth(true), GUILayout.Height(95));

                    using (new EditorGUILayout.HorizontalScope())
                    {
                        EditorGUILayout.LabelField("屏幕方向：" + (ScreenManager.instance.isLandscape ? "横屏" : "竖屏"));
                    }

                    if (!ScreenManager.GameViewIsSimulator())
                    {
                        using (new EditorGUILayout.HorizontalScope())
                        {
                            using (new EditorGUILayout.VerticalScope())
                            {
                                EditorGUILayout.LabelField("刘海屏模拟");
                                _leftOrTopPercent = EditorGUILayout.Slider("左/上百分比", _leftOrTopPercent, 0f, 0.3f);
                                _rightOrBottomPercent = EditorGUILayout.Slider("右/下百分比", _rightOrBottomPercent, 0f, 0.3f);
                                using (new EditorGUILayout.HorizontalScope())
                                {
                                    if (GUILayout.Button("模拟", GUILayout.Width(100)))
                                    {
                                        ScreenUIAdapterMgr.instance.SimulateSafeArea(_leftOrTopPercent, _rightOrBottomPercent);
                                    }
                                    if (GUILayout.Button("还原", GUILayout.Width(100)))
                                    {
                                        ScreenUIAdapterMgr.instance.SimulateSafeArea(0, 0);
                                    }
                                }
                            }
                        }
                    }
                    
                    using (new EditorGUILayout.HorizontalScope())
                    {
                        EditorGUILayout.LabelField("多语言", GUILayout.Width(120));

                        var uiFineTuningMode = ScreenUIAdapterMgr.instance.uiFineTuningMode;
                        var originColor = GUI.color;
                        
                        if (uiFineTuningMode == UIFineTuningMode.None)
                        {
                            GUI.color = Color.cyan;
                        }
                        if (GUILayout.Button("无", GUILayout.Width(100)))
                        {
                            if (uiFineTuningMode != UIFineTuningMode.None)
                            {
                                ScreenUIAdapterMgr.instance.uiFineTuningMode = UIFineTuningMode.None;
                            }
                        }
                        GUI.color = originColor;
                        if (uiFineTuningMode == UIFineTuningMode.CN)
                        {
                            GUI.color = Color.cyan;
                        }
                        if (GUILayout.Button("简中", GUILayout.Width(100)))
                        {
                            if (uiFineTuningMode != UIFineTuningMode.CN)
                            {
                                ScreenUIAdapterMgr.instance.uiFineTuningMode = UIFineTuningMode.CN;
                            }
                        }
                        GUI.color = originColor;
                        if (uiFineTuningMode == UIFineTuningMode.TC)
                        {
                            GUI.color = Color.cyan;
                        }
                        if (GUILayout.Button("繁中", GUILayout.Width(100)))
                        {
                            if (uiFineTuningMode != UIFineTuningMode.TC)
                            {
                                ScreenUIAdapterMgr.instance.uiFineTuningMode = UIFineTuningMode.TC;
                            }
                        }
                        GUI.color = originColor;
                        if (uiFineTuningMode == UIFineTuningMode.EN)
                        {
                            GUI.color = Color.cyan;
                        }
                        if (GUILayout.Button("英文", GUILayout.Width(100)))
                        {
                            if (uiFineTuningMode != UIFineTuningMode.EN)
                            {
                                ScreenUIAdapterMgr.instance.uiFineTuningMode = UIFineTuningMode.EN;
                            }
                        }
                        GUI.color = originColor;
                    }
                }
            }
        }

        private void OnDesignSizeChanged()
        {
            ScreenControllerMgr.RefreshLayout();
        }

        private void OnScreenModeChanged(UIScreenMode screenMode)
        {
            ScreenControllerMgr.RefreshLayout();
        }

        private void OnFineTuningModeChanged(UIFineTuningMode fineTuningMode)
        {
            ScreenControllerMgr.RefreshLayout();
        }
    }
}
